package com.angel.dtoutlis;

import com.angel.bean.*;
import com.angel.bean.dto.BabyInfoDto;
import com.angel.dto.VipUserBabyDto;
import com.angel.service.BabyInfoService;
import com.angel.service.UserInfoService;
import com.angel.service.VipCardService;
import com.angel.service.VipInfoService;

import java.util.ArrayList;
import java.util.List;

public class VipUserBabyDtoUtil {
    public static List<VipUserBabyDto> getVipUserBabyInfos(List<BabyInfoDto> babyInfos, BabyInfoService babyInfoService,
                                                           List<UserInfo> userInfos, UserInfoService userInfoService,
                                                           List<VipInfo> vipInfos, VipInfoService vipInfoService, VipCardService vipCardService, Employee employee) {
        List<VipUserBabyDto> vipUserBabyDtoList;
        if (!CommonUtil.isNull(babyInfos)) {
            vipUserBabyDtoList = VipUserBabyDtoUtil.getVipUserBabyInfosByBabyInfos(babyInfos, userInfoService, vipInfoService, vipCardService,employee);
            return vipUserBabyDtoList;
        } else if (!CommonUtil.isNull(userInfos)) {
            vipUserBabyDtoList = VipUserBabyDtoUtil.getVipUserBabyInfosByUserInfos(userInfos, babyInfoService, vipInfoService, vipCardService,employee);
            return vipUserBabyDtoList;
        } else if (!CommonUtil.isNull(vipInfos)) {
            vipUserBabyDtoList = VipUserBabyDtoUtil.getVipUserBabyInfosByVipInfos(vipInfos, babyInfoService, userInfoService, vipCardService,employee);
            return vipUserBabyDtoList;
        }
        return null;
    }

    /**
     * 通过vip会员卡信息获取用户用户信息
     *
     * @param vipInfos
     * @param babyInfoService
     * @param userInfoService
     * @return
     */
    public static List<VipUserBabyDto> getVipUserBabyInfosByVipInfos(List<VipInfo> vipInfos, BabyInfoService babyInfoService, UserInfoService userInfoService, VipCardService vipCardService,Employee employee) {
        if (CommonUtil.isNull(vipInfos)) {
            return null;
        }
        List<VipUserBabyDto> vipUserBabyDtoList = new ArrayList<>();
        UserInfo userInfo = null;
        BabyInfo babyInfo = null;
        VipCard vipCard = null;
        for (VipInfo vipInfo : vipInfos) {
            if (vipInfo.getUserId() != null && vipInfo.getVipId() != null && vipInfo.getBranId() == employee.getBranchesId()) {
                userInfo = new UserInfo();
                userInfo.setId(vipInfo.getUserId());
                vipCard = new VipCard();
                vipCard.setVipId(vipInfo.getVipId());
                userInfo = userInfoService.findOne(userInfo);
                vipCard = vipCardService.findOne(vipCard);
                if (userInfo != null && userInfo.getBabyId() != null) {
                    babyInfo = new BabyInfo();
                    babyInfo.setBabyId(userInfo.getBabyId());
                    babyInfo = babyInfoService.findOne(babyInfo);
                }
                if (babyInfo != null && vipCard != null) {
                    vipUserBabyDtoList.add(new VipUserBabyDto(userInfo, vipInfo, babyInfo, vipCard));
                }
            }
        }
        if (CommonUtil.isNull(vipUserBabyDtoList)) {
            return null;
        }
        return vipUserBabyDtoList;
    }

    /**
     * 根据宝宝信息获取用户所有信息
     *
     * @param babyInfos
     * @param userInfoService
     * @param vipInfoService
     * @return
     */
    public static List<VipUserBabyDto> getVipUserBabyInfosByBabyInfos(List<BabyInfoDto> babyInfos, UserInfoService userInfoService, VipInfoService vipInfoService, VipCardService vipCardService,Employee employee) {
        if (CommonUtil.isNull(babyInfos)) {
            return null;
        }
        List<VipUserBabyDto> vipUserBabyDtoList = new ArrayList<>();
        UserInfo userInfo = null;
        VipInfo vipInfo = null;
        VipCard vipCard = null;
        for (BabyInfo babyInfo : babyInfos) {
            //通过宝宝信息获取用户信息
            if (babyInfo.getBabyId() != null) {
                userInfo = new UserInfo();
                userInfo.setBabyId(babyInfo.getBabyId());
                userInfo = userInfoService.findOne(userInfo);
                if (userInfo != null && userInfo.getId() != null) {
                    vipInfo = new VipInfo();
                    vipInfo.setUserId(userInfo.getId());
                    vipInfo = vipInfoService.findOne(vipInfo);
                }
                if (vipInfo != null && vipInfo.getVipId() != null && vipInfo.getBranId() == employee.getBranchesId()) {
                    vipCard = new VipCard();
                    vipCard.setVipId(vipInfo.getVipId());
                    vipCard = vipCardService.findOne(vipCard);
                    if (vipCard != null) {
                        vipUserBabyDtoList.add(new VipUserBabyDto(userInfo, vipInfo, babyInfo, vipCard));
                    }

                }
            }
        }
        if (CommonUtil.isNull(vipUserBabyDtoList)) {
            return null;
        }
        return vipUserBabyDtoList;
    }

    /**
     * 根据用户信息获取其他的所有信息
     *
     * @param userInfos
     * @param babyInfoService
     * @param vipInfoService
     * @return
     */
    public static List<VipUserBabyDto> getVipUserBabyInfosByUserInfos(List<UserInfo> userInfos, BabyInfoService babyInfoService, VipInfoService vipInfoService, VipCardService vipCardService,Employee employee) {
        if (CommonUtil.isNull(userInfos)) {
            return null;
        }
        List<VipUserBabyDto> vipUserBabyDtoList = new ArrayList<>();
        BabyInfo babyInfo = null;
        VipInfo vipInfo = null;
        VipCard vipCard = null;
        for (UserInfo userInfo : userInfos) {
            if (userInfo.getBabyId() != null) {
                babyInfo = new BabyInfo();
                babyInfo.setBabyId(userInfo.getBabyId());
                babyInfo = babyInfoService.findOne(babyInfo);
            }
            if (userInfo.getVipId() != null) {
                vipInfo = new VipInfo();
                vipInfo.setUserId(userInfo.getId());
                vipInfo = vipInfoService.findOne(vipInfo);
            }

            if (babyInfo != null && vipInfo != null && vipInfo.getVipId() != null  && vipInfo.getBranId() == employee.getBranchesId()) {
                vipCard = new VipCard();
                vipCard.setVipId(vipInfo.getVipId());
                vipCard = vipCardService.findOne(vipCard);
                if (vipCard != null) {
                    vipUserBabyDtoList.add(new VipUserBabyDto(userInfo, vipInfo, babyInfo, vipCard));
                }
            }

        }

        if (CommonUtil.isNull(vipUserBabyDtoList)) {
            return null;
        }
        return vipUserBabyDtoList;
    }
}
